﻿using Protocol.Code;
using Protocol.Dto;
using Script.Framework;
using Script.Model;
using Script.UI;
using Script.UI.Msg;
using UnityEngine;

namespace Script.Net.Impl
{
    public class MatchHandler : HandlerBase
    {
        private readonly PromptMsg promptMsg = new PromptMsg();

        public override void OnReceive(int subCode, object value)
        {
            switch (subCode)
            {
                case MatchCode.ENTER_SRES:
                    EnterResponse(value as MatchRoomDto);
                    break;
                case MatchCode.ENTER_BRO:
                    EnterBro(value as UserDto);
                    break;
                case MatchCode.LEAVE_BRO:
                    LeaveBro((int) value);
                    break;
                case MatchCode.READY_BRO:
                    ReadyBro((int) value);
                    break;
                case MatchCode.START_BRO:
                    StartBro();
                    break;
                default:
                    Debug.LogError("The have logic error");
                    break;
            }
        }


        /// <summary>
        /// 有玩家准备的广播处理
        /// </summary>
        /// <param name="readUserId"></param>
        private void ReadyBro(int readUserId)
        {
            // 保存数据
            Models.GameModel.MatchRoomDto.Ready(readUserId);

            // 通知UI脚本更新
            Dispatch(AreaCode.UI, UIEvent.PLAYER_READY, readUserId);

            if (readUserId == Models.GameModel.UserDto.Id)
            {
                // 发送消息 隐藏自己的准备按钮 防止多次点击 和服务器交互
                Dispatch(AreaCode.UI, UIEvent.PLAYER_HIDE_READY_BUTTON, null);
            }
        }

        /// <summary>
        /// 游戏开始的广播处理
        /// </summary>
        private void StartBro()
        {
            promptMsg.Change("所有玩家准备开始游戏", Color.blue);
            Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
            // 开始游戏
            // 隐藏状态面板的准备文字
            Dispatch(AreaCode.UI, UIEvent.PLAYER_HIDE_STATE, null);
        }

        /// <summary>
        /// 有人离开匹配房间的广播处理
        /// </summary>
        /// <param name="leaveUserId"></param>
        private void LeaveBro(int leaveUserId)
        {
            Dispatch(AreaCode.UI, UIEvent.PLAYER_LEAVE, leaveUserId);

            ResetPosition();

            // 保存数据
            Models.GameModel.MatchRoomDto.Leave(leaveUserId);
        }

        /// <summary>
        /// 有人进入房间的广播处理
        /// </summary>
        /// <param name="newUser"></param>
        private void EnterBro(UserDto newUser)
        {
            // 跟新房间数据
            Models.GameModel.MatchRoomDto.Add(newUser);

            ResetPosition();

            // 给UI绑定数据
            Dispatch(AreaCode.UI, UIEvent.PLAYER_ENTER, newUser.Id);

            // 给用户一个提示
            promptMsg.Change("有新玩家（" + newUser.Name + ")进入", Color.blue);
            Debug.LogError("其它玩家确实进入了");
            Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
        }

        /// <summary>
        /// 自身进入的服务器响应
        /// </summary>
        /// <param name="matchRoomDto"></param>
        private void EnterResponse(MatchRoomDto matchRoomDto)
        {
            Models.GameModel.MatchRoomDto = matchRoomDto;

            ResetPosition();
            Dispatch(AreaCode.UI, UIEvent.SHOW_ENTER_ROOM_BUTTON, null);
        }

        /// <summary>
        /// 重置位置
        /// 跟新左右玩家显示
        /// </summary>
        private void ResetPosition()
        {
            Models.GameModel.MatchRoomDto.ResetPosition(Models.GameModel.UserDto.Id);

            MatchRoomDto matchRoomDto = Models.GameModel.MatchRoomDto;

            Debug.Log("现在一共有" + matchRoomDto.UIdList.Count + "个人");
            Debug.Log("leftID=" + matchRoomDto.LeftId);
            Debug.Log("rightID=" + matchRoomDto.RightId);

            // 显示现在在房间内的玩家
            if (matchRoomDto.LeftId != -1)
            {
                UserDto leftUserDto = matchRoomDto.UIdUserDict[matchRoomDto.LeftId];
                Dispatch(AreaCode.UI, UIEvent.SET_LEFT_PLAYER_DATA, leftUserDto);
            }

            if (matchRoomDto.RightId != -1)
            {
                UserDto rightUserDto = matchRoomDto.UIdUserDict[matchRoomDto.RightId];
                Dispatch(AreaCode.UI, UIEvent.SET_RIGHT_PLAYER_DATA, rightUserDto);
            }

            // 给自己绑定数据
            // UserDto myUserDto = room.UIdUserDict[Models.GameModel.UserDto.Id];
            // Dispatch(AreaCode.UI, UiEvent.SET_MY_PLAYER_DATA, myUserDto);
        }
    }
}